package com.blogs.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.blogs.entity.AppConfig;
import com.blogs.entity.Feed;
import com.blogs.entity.MsgEntity;
import com.blogs.tools.MsgCountToast;
import com.blogs.tools.TbBlogLine;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GetTimeLine{
	public interface IMsgCallBack {
		public void onSuccess(ArrayList<Feed> feedList);
		public void onFailed();
	}
	private TbBlogLine db;
	private String url=AppConfig.GETTIMELINE_URL;
	private String since_id;
	private GetMsgTask timeLine;
    private IMsgCallBack callback;
    private boolean isCache;
    private boolean isMore;
    private boolean isPull;
    private Context context;
	public GetTimeLine(Context context, String channel,int page,String since_id,String max_id,IMsgCallBack callback)
	{
		this.context=context;
		this.callback=callback;
		timeLine=new GetMsgTask(taskCallBack);
		
		try {
			channel=URLEncoder.encode(channel,"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		url+="&page="+page+"&since_id="+since_id+"&max_id="+max_id+"&channelpath="+channel+"&t="+new Date().getTime();
		this.since_id=since_id;
		isCache=since_id.equals("")&&max_id.equals("");
		isMore=!max_id.equals("");
		isPull=!since_id.equals("");
		db=new TbBlogLine(context, channel);
	}
	public void loadData()
	{
		if(db.RowCount()>0&&isCache)
		{
			callback.onSuccess(db.Select());
			db.DbClose();
		}
		else
		{
			timeLine.execute(url);
		}
	}
	private GetMsgTask.onTaskCallBack taskCallBack=new GetMsgTask.onTaskCallBack() {
		
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			
			
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<ArrayList<Object>> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				if(db!=null)
				{
					db.DbClose();
				}
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				if(db!=null)
				{
					db.DbClose();
				}
				return;
			}
			ArrayList<Feed> feeds=new ArrayList<Feed>();
			Iterator<Object> iterator = msg.data.iterator();
			int i=0;
			while (iterator.hasNext()) 
			{
				i++;
				Map<String, Object> objs = (Map<String, Object>) iterator.next();
				Feed feed = new Feed(objs);
				feeds.add(feed);
			}
			if(!isMore)
			{
				db.Delete();
				db.Insert(feeds);
			}
			if(isPull)
			{
				int fcount=0;
				for(Feed f :feeds)
				{
					if(since_id.equals(f.blog_id))
					{
						break;
					}
					fcount++;
				}
				MsgCountToast.ShowToast(context, fcount);
			}
			db.DbClose();
			Log.i("feeds", feeds.size()+","+i);
			callback.onSuccess(feeds);
			Log.i("GetMsgTask", "Success");
		}
		
		public void onFailed() {
			// TODO Auto-generated method stub
			callback.onFailed();
			db.DbClose();
			Log.i("GetMsgTask", "Failed");
		}
	};
}
